$(function(){
	//changeQuestion('radio');
	var now_question_type = parseInt($('select[name=questions_type]').val());
	var nowquesid = parseInt($('#question_id').val());
	if(typeof(now_question_type) == 'undefined' || isNaN(now_question_type) || isNaN(nowquesid )|| typeof(nowquesid) == 'undefined' || nowquesid == 0 ) {
		changeQuestion('radio');
	} else {
		updateOptions(now_question_type,nowquesid);
	}
	$(document).on('click','.itembank-search', function () {
		var id = $(this).attr('data-id');
		var container = $('#question-list-container');
		var questionsTemplate = _.template($('#question-list-template').html());
		var text = $("#item_"+id).find('.title-l').text();
		$('#question-list').find('.modal-title').text(text);
		container.html('');
		$(".holder").html('');
		questions(id,1,questionsTemplate,container);
		$('#question-list').modal('show');
	});
	
	$("#add-question").on('change','.question-type',function(){
		var value = parseInt($(this).val());
		switch(value){
		case 0:
			changeQuestion('radio');break;
		case 1:
			changeQuestion('checkbox');break;
		case 2:
			changeQuestion('judge');break;
		case 3:
			changeQuestion('text');break;
		}
	});
	
	$(document).on('click','.add-option',function(){
		
        var option = getOptions('radio');
        var type = $('.question-type').val();
        if(type == 1){
        	option = getOptions('checkbox');
        	type = 'checkbox';
        }else{
        	type='radio';
        }
        if(isComEmpty(option)){
        	option = 1;
        }
        if(option > 10) {
        	layer.open({
        		 title: '系统提示',
                content: '最多10个选项'
        	});
        	return false;
        }
        var container = $('#question-options-group');
        var optionTemplate = _.template($('#'+type+'-item-template').html());
    	var dataVal = changeIntToEng2(option);
        container.append(optionTemplate({option:option,dataVal:dataVal}));
        
        inintIcheck();
        
        questionEditorInit();
	});
	
	$(document).on('click','.option-remove',function(){
		$(this).parent().parent().remove();
	});
	
	$('#add-question').on('click','.question-submit', function () {
		var form = $('#question-item-form');
		var answer='';
		var question_type = form.find('select[name=questions_type]').val()
		if(question_type == 1){
			form.find('input:checkbox').each(function() {
				if($(this)[0].checked == true) {
					answer += changeIntToEng2(parseInt($(this).val()));
				}
			});
		}else if(question_type == 3){
			answer = form.find('textarea[name=answer]').val();
		}else{
			answer = changeIntToEng2(parseInt(form.find('input[name=answer]:checked').val()));
		}
		
		var itembankId = $('#itembank_id').val();
		
		var data = {
			itembank_id:itembankId,
			title:form.find('textarea[name=title]').val(),
            answer:answer,
			question_id:$('#question_id').val(),
            explain:form.find('textarea[name=explain]').val(),
            questions_type:question_type,
        /*    skill:form.find('input[name=skill]').val(),
            jobstype:form.find('input[name=jobstype]').val(),*/
			options:formDataParse(question_type),	
		};
		//$('.loading').show();
		$.ajax({
			'url':'/biz/question/store',
	        'type':'POST',
	        'dataType': 'json',
	        'data':data,
	        error: function(data) {
	        	$('.loading').hide();
	            var response = data.responseJSON;
	            var errors = '';
	            $.each(response,function(k,v){
	                errors +="<div>"+v+"</div>";
	            })
	            layer.open({
	                title: '系统提示',
	                content: errors
	            });
	        },
	        success: function(data){
	            if(data.status == 'success'){
	            	$('.loading').hide();
	            	editors.html('');
	        		exp_editors.html('');
            		form.get(0).reset();
	            	$('#add-question').modal('hide');
	            	var nowVal = parseInt($('#item_'+itembankId).find('.question_count').text());
	            	$('#item_'+itembankId).find('.question_count').text(nowVal+1);
	            	layer.msg(data.msg,{time: 1000});
					window.location.reload();
	            }else{
	            	$('.loading').hide();
	                layer.open({
	                    title: '系统提示',
	                    content: data.msg
	                });
	            }
	        }
		});
	});

	$('#addModel').on('click','.create-itembank', function () {
		var data = $("#itembank-form").serialize();
		$("#itembank-form").get(0).reset();
		var itemTemplate = _.template($('#items-template').html());
		var container = $('#company-exam-list');
		$('#addModel').modal('hide');
		$('.loading').show();
		$.ajax({
	        'url':'/biz/index/store',
	        'type':'POST',
	        'data':data,
	        error: function(data) {
	        	$('.loading').hide();
	            var response = data.responseJSON;
	            var errors = '';
	            $.each(response,function(k,v){
	                errors +="<div>"+v+"</div>";
	            })
	            layer.open({
	                title: '系统提示',
	                content: errors
	            });
	        },
	        success: function(data){
	        	$('.loading').hide();
	            if(data.status == 'success'){
	            	//container.prepend(itemTemplate({item:data.data}));
	            	layer.msg(data.msg,{time: 1000});
					window.location.reload();
	            }else{
	                layer.open({
	                    title: '系统提示',
	                    content: data.msg
	                });
	            }
	        }
	    });
	});
	$(document).on('click','.itembank-remove', function (){
		var id = $(this).attr('data-id');
		var text = $("#item_"+id).find('.title-l').text();
		BootstrapDialog.confirm({
	        title: '确认删除',
	        message:'确定要删除［'+text+'］吗?',
	        
	        callback:function(result){
	            if(result) {
					$.ajax({
						'url':'/biz/index/destroy/'+id,
				        'type':'DELETE',
				        error: function(data) {
				            layer.open({
				                title: '系统提示',
				                content: '网络异常...'
				            });
				        },
				        success: function(data){
				            if(data.status == 'success'){
				            	$('#item_'+id).remove();
				            	layer.msg(data.msg,{time: 1000});
				            }else{
				                layer.open({
				                    title: '系统提示',
				                    content: data.msg
				                });
				            }
				        }
					});
	            }
	        }
		}).setType(BootstrapDialog.TYPE_DEFAULT);
	});
	$('#question-list').on('click','.question-list-trash',function(){
		var id = $(this).attr('data-id');
		var text = $(this).attr('data-value');
		BootstrapDialog.confirm({
	        title: '确认删除',
	        message:'确定要删除［'+text+'］吗?',
	        callback:function(result){
	            if(result) {
					$.ajax({
						'url':'/biz/question/destroy/'+id,
				        'type':'DELETE',
				        error: function(data) {
				            layer.open({
				                title: '系统提示',
				                content: '网络异常...'
				            });
				        },
				        success: function(data){
				            if(data.status == 'success'){
				            	$('#question_list_item_'+id).remove();
				            	layer.msg(data.msg,{time: 1000});
				            }else{
				                layer.open({
				                    title: '系统提示',
				                    content: data.msg
				                });
				            }
				        }
					});
	            }
	        }
		}).setType(BootstrapDialog.TYPE_DEFAULT);
	});
	$(document).on('click','.add-question', function () {
		var id = $(this).attr('data-id');
		var form = $('#question-item-form');
		var disabled = $(this).attr('disabled');
		if(disabled){
			return false;
		}
		$('#itembank_id').val(id);
		form.find('.question-type').val(0);
		$('#questions_type').selectpicker('refresh');
		questionEditorInit();
		changeQuestion('radio');
		$('#add-question').modal('show');
	});
	
	var jobstypes = $('input[name=jobstypes]').val().split(',');
	var skills = $('input[name=skills]').val().split(',');
    $('#jobstype').autocomplete({
		hints: jobstypes,
		width: '100%',
		height: 43,
		showButton:false,
		inputName:'jobstype',
	});
    $('#skills').autocomplete({
		hints: skills,
		width: '100%',
		height: 43,
		showButton:false,
		inputName:'skill',
	});
});

function updateOptions(value,id)
{
	switch(value){
		case 0:
			updateQuestion('radio',id);break;
		case 1:
			updateQuestion('checkbox',id);break;
		case 2:
			updateQuestion('judge',id);break;
		case 3:
			updateQuestion('text',id);break;
		default:
			updateQuestion('radio',id);
	}
}

function updateQuestion(type,id){
	var container = $('#question-option-container');

	var optionTemplate = _.template($("#"+type+"-update-template").html());

	var params = {
		url: '/admin/enterprise/questions/ques-info/'+id,
		type: 'GET',
		dataType: 'json',
		data:{},
	};
	$.ajax(params).done(function(resp){
		container.html(optionTemplate({item:resp}));
		inintIcheck();
		questionEditorInit();
	});

	inintIcheck();
	questionEditorInit();
}
